var md = require('../../utils/md5.js');

var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
var app = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNumber: "",
    verifyCode: "获取验证码",
    is_disable: false,
    verifyCodeNumber: "",
    pwdNumber: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  bindPhoneInput: function (e) {
    this.phoneNumber = e.detail.value;
  },

  bindPwdInput: function (e) {
    this.pwdNumber = e.detail.value;
  },

  bindVerifyCodeInput: function (e) {
    this.verifyCodeNumber = e.detail.value;

  },
  // 提交修改密码
  submitAction: function (e) {

    var isphone = util.validateMobile(this.phoneNumber);
    if (!isphone) {
      util.showError('请输入正确的手机号')
      return;
    }


    if (util.isEmpty(this.pwdNumber)) {
      util.showError('请输入密码')
      return;
    }

    if (util.isEmpty(this.verifyCodeNumber)) {
      util.showError('请输入验证码')
      return;
    }

    var params = new Object()
    params.captcha = this.verifyCodeNumber;
    params.mobile = this.phoneNumber;
    params.password = md(this.pwdNumber);

    util.showLoading("密码修改中...");
    var that = this;
    api.POST({
      params: params,
      url: app.globalData.forgetPwd_url,
      success: function (res) {
        var message = res.data.message;
        if (res.data.code) {
          util.showError(message);
          return;
        } else {
          wx.navigateBack({

          })
        }
      },
      fail: function (fail) {
        util.showError("网络错误，请检查网络");
      }
    })
  },

  // 获取验证码
  getVerifyCodeAction: function () {
    var isphone = util.validateMobile(this.phoneNumber);
    if (!isphone) {
      util.showError('请输入正确的手机号')
      return;
    }
    var params = new Object()
    params.type = 2;
    params.user_type = 1;
    params.mobile = this.phoneNumber;

    util.showLoading("验证码获取中...");
    var that = this;
    api.POST({
      params: params,
      url: app.globalData.verifyCode_url,
      success: function (res) {
        var message = res.data.message;
        if (res.data.code) {
          util.showError(message);
          return;
        }else{
          var total_micro_second = 60 * 1000;
          //验证码倒计时
          count_down(that, total_micro_second);
          wx.showModal({
            title: '提示',
            content: '发送验证码成功！',
            showCancel: false
          })
          util.hiddenLoading();
        }
      },
      fail: function (fail) {
        util.showError("网络错误，请检查网络");
      }
    })

  },
})


/* 毫秒级倒计时 */
function count_down(that, total_micro_second) {
  if (total_micro_second <= 0) {
    that.setData({
      verifyCode: "重新发送",
      is_disable: false,

    });
    // timeout则跳出递归
    return;
  }
  // 渲染倒计时时钟
  that.setData({
    verifyCode: date_format(total_micro_second) + " 秒",
    is_disable: true,

  });
  setTimeout(function () {
    // 放在最后--
    total_micro_second -= 10;
    count_down(that, total_micro_second);
  }, 10)
}
// 时间格式化输出，如03:25:19 86。每10ms都会调用一次
function date_format(micro_second) {
  // 秒数
  var second = Math.floor(micro_second / 1000);
  // 小时位
  var hr = Math.floor(second / 3600);
  // 分钟位
  var min = fill_zero_prefix(Math.floor((second - hr * 3600) / 60));
  // 秒位
  var sec = fill_zero_prefix((second - hr * 3600 - min * 60));// equal to => var sec = second % 60;
  // 毫秒位，保留2位
  var micro_sec = fill_zero_prefix(Math.floor((micro_second % 1000) / 10));
  return sec;
}
// 位数不足补零
function fill_zero_prefix(num) {
  return num < 10 ? "0" + num : num
}